﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;

namespace ath2011_sql_WPF
{
    class results
    {
        public string[] ShowResults(string constr, string command)
        {
            SqlConnection con1;
            SqlCommand cmd1;
            string[] result1;
            int rows = 0;

            try
            {
                con1 = new SqlConnection(constr);
                con1.Open();
                cmd1 = new SqlCommand(command, con1);
                int rozm = 0;
                SqlDataReader rd, rd1;
                int i = 10;

                try
                {
                    rd1 = cmd1.ExecuteReader();

                    while (rd1.Read())
                    {
                        rozm++;
                        Console.WriteLine(rozm + "---------");
                    }
                    rd1.Close();

                    rd = cmd1.ExecuteReader();



                    result1 = new string[rozm + 1];


                    while (rd.Read())
                    {
                        //rozm++;
                        int columns = rd.FieldCount;

                        for (int a = 0; a < columns; a++)
                        {
                            result1[i] += rd[a].ToString() + " ";
                        }

                        Console.WriteLine(result1[i] + " " + i);
                        i++;
                    }

                    rd.Close();
                }
                catch (Exception e)
                {
                    result1 = new string[1];
                    result1[0] = "wrong SQL command or content does not exist+/r/n" + e;
                }
                finally
                {
                    if (rozm == 0)
                    {
                        result1 = new string[2];
                        result1[0] = "Command executed succesfuly !";

                    }
                }

                con1.Close();
            }
            catch (Exception e)
            {
                result1 = new string[1];
                result1[0] = "wrong SQL connection string";
            }
            finally
            { }

            return result1;
        }
    }
}
